<?php
require_once("mysql.inc.php");

if(isset($_GET["author"])) {
	getBooks($_GET["author"]);
}

function getBooks($author) {
	
	$author = mysql_real_escape_string($author);
	$result = mysql_query("SELECT * FROM `books` WHERE `author`='$author' ORDER BY `title`") or die(mysql_error());
	$num = mysql_num_rows($result);
	$i = 1;
	
	//create new dom document
	$doc = new DomDocument('1.0');
	$root = $doc->createElement("results");
	$doc->appendChild($root);
	
	//create first element containing amount of results
	$n = $doc->createElement("num");
	$n->appendChild($doc->createTextNode($num));
	$root->appendChild($n);
	
	while($fetch = mysql_fetch_array($result)) {
		
		$b = $doc->createElement("book$i");
		$b->appendChild($doc->createTextNode($fetch["title"]));
		$root->appendChild($b);
		
		$i++;
	}
	
	echo $doc->saveXML();
}
?>